package main

import (
	"algorithm/Queue"
	"algorithm/StackArray"
	"fmt"
	"io/ioutil"
	"log"
	"strings"
)

// 目录文件扫描
func main1() {
	dir := "D:\\go\\test"
	listFiles(dir, 0)
}

//level用来记录当前递归的层次，生成带有层次感的空格
func listFiles(dirname string, level int) {
	s1 := "+--"
	for i := 0; i < level; i++ {
		s1 = "+--" + s1
	}

	fileinfo, err := ioutil.ReadDir(dirname)
	if err != nil {
		log.Fatal(err)
		return
	}

	filename := ""
	for _, info := range fileinfo {
		filename = dirname + "\\" + info.Name()
		fmt.Printf("%s\n", filename)
		if info.IsDir() {
			listFiles(filename, level+1)
		}
	}
}

// 栈的文件遍历
func main2() {

	fmt.Println("栈")
	dir := "D:\\go\\test"

	files := []string{}

	stack := StackArray.MyStack()
	stack.Push(dir)

	for !stack.IsEmpty() {
		path := stack.Pop().(string)

		files = append(files, path) //加入文件

		pathInfo, _ := ioutil.ReadDir(path)
		for _, info := range pathInfo {
			fileName := info.Name()
			if info.IsDir() {

				stack.Push(path + "\\" + fileName)

			} else {
				files = append(files, path+"\\"+fileName)
			}
		}

	}

	for i := 0; i < len(files); i++ {
		fmt.Println(files[i])
	}

}

//队列
func main() {
	fmt.Println("队列")
	dir := "D:\\go\\test"

	files := []string{}

	queue := Queue.NewQueue()
	queue.EnQueue(dir)

	for {

		path := queue.DeQuue()
		// fmt.Println(path.(string))
		if path == nil {
			break
		}

		read, _ := ioutil.ReadDir(path.(string))

		for _, v := range read {

			if v.IsDir() {

				fulldir := path.(string) + "\\" + v.Name()
				queue.EnQueue(fulldir)
				files = append(files, fulldir)
			} else {

				// 只加载.txt 文件
				if strings.Contains(v.Name(), ".txt") {

					fulldir := path.(string) + "\\" + v.Name()
					files = append(files, fulldir)
				}

			}
		}

	}

	for i := 0; i < len(files); i++ {
		fmt.Println(files[i])
	}
}
